package com.customer.crm.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.customer.crm.system.pojo.SystemUser;
import com.customer.crm.system.pojo.Task;
import com.customer.crm.system.service.SystemUserService;
import com.customer.crm.system.service.TaskService;
import com.customer.crm.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * The type Task controller.
 *
 * @author Lyp
 * @create 2021 /12/20
 */
@Controller
public class TaskController {


    /**
     * The Task service.
     */
    @Autowired
    private TaskService taskService;

    /**
     * The System user service.
     */
    @Autowired
    private SystemUserService systemUserService;

    /**
     * To task string.
     * 去任务页面
     *
     * @return the string
     */
    @RequestMapping("/system/task/TaskAction_list.action")
    public String toTask() {

        return "views/manage/task/list_task";
    }

    /**
     * Find by page model map.
     * 查询所有任务信息
     *
     * @param page the page
     * @param rows the rows
     * @return the model map
     */
    @RequestMapping("/manage/task/TaskAction_findByPage.action")
    @ResponseBody
    public ModelMap findByPage(Long page, Long rows) {
        page = page  -1 ;
        List<Task> list = taskService.findAllTask(page * rows, rows, null);
        List<Task> list1 = taskService.list(new QueryWrapper<Task>());

        ModelMap modelMap = new ModelMap();

        modelMap.put("totalRows", list1.size());
        modelMap.put("result", list);

        ModelMap modelMap1 = new ModelMap();
        modelMap1.put("data", modelMap);
        return modelMap1;
    }

    /**
     * See task action string.
     * 查看信息
     *
     * @param taskId   the task id
     * @param modelMap the model map
     * @return the string
     */
    @RequestMapping("/manage/task/TaskAction_see.action")
    public String seeTaskAction(@RequestParam("id") Integer taskId, ModelMap modelMap) {

        List<Task> list = taskService.findAllTask(null, null, taskId);

        modelMap.put("task", list.get(0));
        return "views/manage/task/see_task";
    }

    /**
     * Modify string.
     * 去修改页面
     *
     * @param taskId   the task id
     * @param modelMap the model map
     * @return the string
     */
    @RequestMapping("/manage/task/TaskAction_modify.action")
    public String modify(@RequestParam("id") Integer taskId,
                         ModelMap modelMap) {
        List<Task> list = taskService.findAllTask(null, null, taskId);
        modelMap.put("task", list.get(0));

        List<SystemUser> systemUsers = systemUserService.list();
        modelMap.put("listSys", systemUsers);

        return "views/manage/task/modify_task";
    }

    /**
     * Update task action string.
     * 修改操作
     *
     * @param task the task
     * @return the string
     */
    @RequestMapping("/manage/task/TaskAction_update.action")
    @ResponseBody
    @Transactional(rollbackFor = {})
    public ResultData updateTaskAction(Task task) {
        QueryWrapper<Task> wrapper = new QueryWrapper<>();
        wrapper.eq("id", task.getId());
        boolean update = taskService.update(task, wrapper);
        return ResultData.buildSuccessResult("修改成功", update);
    }


    /**
     * To edit string.
     * 去添加页面
     *
     * @param modelMap the model map
     * @return the string
     */
    @RequestMapping("/manage/task/TaskAction_edit.action")
    public String toEdit(ModelMap modelMap) {
        List<SystemUser> systemUsers = systemUserService.list();
        modelMap.put("list", systemUsers);
        return "views/manage/task/edit_task";
    }

    /**
     * Save task result data.
     * 添加操作
     *
     * @param task the task
     * @return the result data
     */
    @RequestMapping("/manage/task/TaskAction_save.action")
    @ResponseBody
    @Transactional(rollbackFor = {})
    public ResultData saveTask(Task task) {
        int id = (int) (Math.random() * 1000);
        task.setId(id);
        System.out.println(task);
        boolean save = taskService.save(task);
        return ResultData.buildSuccessResult("保存成功", save);
    }

    /**
     * Delete result data.
     * 删除选中
     *
     * @param ids the ids
     * @return the result data
     */
    @RequestMapping("/manage/task/TaskAction_del.action")
    @ResponseBody
    @Transactional(rollbackFor = {IllegalArgumentException.class})
    public ResultData delete(String[] ids) {
        QueryWrapper<Task> wrapper = new QueryWrapper<>();
        boolean remove = false;
        for (String id : ids) {
            wrapper.eq("id", id);
            remove = taskService.remove(wrapper);
        }
        return ResultData.buildSuccessResult("删除成功", remove);
    }

        /**
         * My task model map.
         * 我的任务
         *
         * @param session the session
         * @return the model map
         */
        @RequestMapping("/manage/task/TaskAction_myTask.action")
        @ResponseBody
        public ModelMap myTask (HttpSession session){
            ModelMap modelMap = new ModelMap();
            SystemUser systemUser = (SystemUser) session.getAttribute("systemUser");
            List<Task> list = taskService.findMyTask(systemUser.getUserName());
            modelMap.put("totalRows", list.size());
            modelMap.put("result", list);

            ModelMap modelMap1 = new ModelMap();
            modelMap1.put("data", modelMap);
            return modelMap1;
        }

        /**
         * My distribute task model map.
         * 我分配的任务
         *
         * @param session the session
         * @return the model map
         */
        @RequestMapping(value = "/manage/task/TaskAction_myDistributeTask.action", method = RequestMethod.POST)
        @ResponseBody
        public ModelMap myDistributeTask (HttpSession session){
            ModelMap modelMap = new ModelMap();
            SystemUser systemUser = (SystemUser) session.getAttribute("systemUser");
            List<Task> list = taskService.myDistributeTask(systemUser.getUserName());
            modelMap.put("totalRows", list.size());
            modelMap.put("result", list);

            ModelMap modelMap1 = new ModelMap();
            modelMap1.put("data", modelMap);
            return modelMap1;
        }

        /**
         * Not initiated model map.
         * 未启动
         *
         * @param session the session
         * @return the model map
         */
        @RequestMapping("/manage/task/TaskAction_notInitiated.action")
        @ResponseBody
        public ModelMap notInitiated (HttpSession session){
            ModelMap modelMap = new ModelMap();
            System.out.println("请求过来了");
            SystemUser systemUser = (SystemUser) session.getAttribute("systemUser");

            return modelMap;
        }


        /**
         * Put off model map.
         * 推迟
         *
         * @param session the session
         * @return the model map
         */
        @RequestMapping("/manage/task/TaskAction_putOff.action")
        @ResponseBody
        public ModelMap putOff (HttpSession session){
            ModelMap modelMap = new ModelMap();
            System.out.println("请求过来了");
            SystemUser systemUser = (SystemUser) session.getAttribute("systemUser");

            return modelMap;
        }


        /**
         * In process model map.
         * 进行中
         *
         * @param session the session
         * @return the model map
         */
        @RequestMapping("/manage/task/TaskAction_inProcess.action")
        @ResponseBody
        public ModelMap inProcess (HttpSession session){
            ModelMap modelMap = new ModelMap();
            System.out.println("请求过来了");
            SystemUser systemUser = (SystemUser) session.getAttribute("systemUser");

            return modelMap;
        }

        /**
         * Completed model map.
         * 已完成
         *
         * @param session the session
         * @return the model map
         */
        @RequestMapping("/manage/task/TaskAction_completed.action")
        @ResponseBody
        public ModelMap completed (HttpSession session){
            ModelMap modelMap = new ModelMap();
            System.out.println("请求过来了");
            SystemUser systemUser = (SystemUser) session.getAttribute("systemUser");

            return modelMap;
        }


        /**
         * Closed model map.
         * 关闭的
         *
         * @param session the session
         * @return the model map
         */
        @RequestMapping("/manage/task/TaskAction_closed.action")
        @ResponseBody
        public ModelMap closed (HttpSession session){
            ModelMap modelMap = new ModelMap();
            System.out.println("请求过来了");
            SystemUser systemUser = (SystemUser) session.getAttribute("systemUser");

            return modelMap;
        }


        /**
         * End today model map.
         * 今日截止
         *
         *
         * @return the model map
         */
        @RequestMapping("/manage/task/TaskAction_endToday.action")
        @ResponseBody
        public ModelMap endToday (){
            ModelMap modelMap = new ModelMap();
            List<Task> list = taskService.endToday();
            modelMap.put("totalRows", list.size());
            modelMap.put("result", list);

            ModelMap modelMap1 = new ModelMap();
            modelMap1.put("data", modelMap);
            return modelMap1;
        }

        /**
         * End week model map.
         * 本周截止
         *
         *
         * @return the model map
         */
        @RequestMapping("/manage/task/TaskAction_endWeek.action")
        @ResponseBody
        public ModelMap endWeek (){
            ModelMap modelMap = new ModelMap();
            List<Task> list = taskService.endWeek();
            modelMap.put("totalRows", list.size());
            modelMap.put("result", list);

            ModelMap modelMap1 = new ModelMap();
            modelMap1.put("data", modelMap);
            return modelMap1;
        }


        /**
         * End month model map.
         * 本月截止
         *
         * @param session the session
         * @return the model map
         */
        @RequestMapping("/manage/task/TaskAction_endMonth.action")
        @ResponseBody
        public ModelMap endMonth (HttpSession session){
            ModelMap modelMap = new ModelMap();
            List<Task> list = taskService.endMonth();
            modelMap.put("totalRows", list.size());
            modelMap.put("result", list);

            ModelMap modelMap1 = new ModelMap();
            modelMap1.put("data", modelMap);
            return modelMap1;
        }


        /**
         * Recently assigned model map.
         * 最近分配
         *
         * @param session the session
         * @return the model map
         */
        @RequestMapping("/manage/task/TaskAction_recentlyAssigned.action")
        @ResponseBody
        public ModelMap recentlyAssigned (HttpSession session){
            ModelMap modelMap = new ModelMap();
            System.out.println("请求过来了");
            SystemUser systemUser = (SystemUser) session.getAttribute("systemUser");

            return modelMap;
        }


        /**
         * Recently updated model map.
         * 最近更新
         *
         * @param session the session
         * @return the model map
         */
        @RequestMapping("/manage/task/TaskAction_recentlyUpdated.action")
        @ResponseBody
        public ModelMap recentlyUpdated (HttpSession session){
            ModelMap modelMap = new ModelMap();
            System.out.println("请求过来了");
            SystemUser systemUser = (SystemUser) session.getAttribute("systemUser");

            return modelMap;
        }


        @RequestMapping("/manage/task/TaskAction_search.action")
        public String search(@RequestParam("field") String field){
            System.out.println("消息过来了" + field);
            return "";

            //'readystatechange' handler took
        }
    }
